package com.ymatou.iapi.godmch.testcase;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.ymatou.iapi.godmch.parameter.getConditionMarkupConfigListBean;
import com.ymatou.iapi.godmch.parameter.setConditionMarkupConfigBean;
import com.ymatou.iapi.godmch.service.getConditionMarkupConfigListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.markup_config_condition;
import com.ymttest.database.model.godmch.markup_config_conditionExample;
import com.ymttest.database.sqlwapper.godmch.markup_config_conditionWapper;
import com.ymttest.utils.TestDataManager;
/**
*按条件自动加价配置列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getConditionMarkupConfigList{
	private static getConditionMarkupConfigListBean bean;
	private static getConditionMarkupConfigListCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("按条件自动加价配置列表");
	}

	@Before
	public void caseUp() {
		bean=new getConditionMarkupConfigListBean();
		call=new getConditionMarkupConfigListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private markup_config_condition selectConfig(Integer agentId,Integer supplierId,Integer... markupId) throws InterruptedException
	{
		markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		markup_config_conditionExample example=new markup_config_conditionExample();
		example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		if(markupId.length>0)
			example.createCriteria().andMarkup_idEqualTo(markupId[0]);
		
		example.setOrderByClause(" create_time desc");
		markup_config_condition config=markupConfig.selectByExample(example).get(0);
		return config;
	}
	private void updateTimeConfig(Integer agentId,Integer supplierId) throws InterruptedException
	{
		   //修改update_time 因为相同的agentid+suppelierId 1分钟内无法修改
		   markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
		   markup_config_conditionExample example=new markup_config_conditionExample();
		   example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
		   markup_config_condition config=markupConfig.selectByExample(example).get(0);
		   config.setUpdate_time(Date.from((LocalDateTime.now().plusDays(-1).atZone(ZoneId.systemDefault()).toInstant())));
		   markupConfig.updateByPrimaryKey(config);
	}
	private void check_verify(Integer agentId,Integer supplierId) throws InterruptedException {
		
		 Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		 JSONArray jArray=call.getList();
		 Logger.verifyEquals(jArray.size(), call.getTotal(), "验证total与返回的一致");

			markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
			markup_config_conditionExample example=new markup_config_conditionExample();
			example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
			example.setOrderByClause(" create_time desc");
			List<markup_config_condition>  configs=markupConfig.selectByExample(example);
			for(int i=0;i<configs.size();i++){
				markup_config_condition config=configs.get(i);
				JSONObject job=jArray.getJSONObject(i);
				 Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
				
				 Logger.verifyEquals(Integer.parseInt(job.get("markupType").toString()), config.getMarkup_type(), "验证数据库中的Markup_type与返回的一致");
				 Logger.verifyEquals(new BigDecimal(job.get("percentage").toString()).stripTrailingZeros().toPlainString(), config.getPercentage().stripTrailingZeros().toPlainString(), "验证数据库中的percentage与返回的一致");
				 Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
				 Logger.verifyEquals(Integer.parseInt(job.get("onSaleType").toString()), config.getOn_sale_type(), "验证数据库中的onSaleType与返回的一致");
				 Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
			}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_001() {
		Logger.start(true,"按条件自动加价配置列表-一条类型-返回成功");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	           
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setPageIndex(1);
	         bean.setPageSize(2);
	         
			 call.setData(bean);
		 	 call.callService();		
		 	check_verify(agentId,supplierId);
				 
				 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_002() {
		Logger.start(true,"按条件自动加价配置列表-设置两条类型-返回成功");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
			  //第一条
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         //第二条
	         setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setPageIndex(1);
	         bean.setPageSize(2);
	         
			 call.setData(bean);
		 	 call.callService();	
		 	 
			 check_verify(agentId,supplierId);
			 	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_003() {
		Logger.start(true,"按条件自动加价配置列表-中间一页显示-返回成功");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
			  //第一条
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         //第二条
	         setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         
	         //修改时间
	         markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
			 markup_config_conditionExample example=new markup_config_conditionExample();
			 example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
			 markup_config_condition config=markupConfig.selectByExample(example).get(1);
			 config.setUpdate_time(Date.from((LocalDateTime.now().plusDays(-1).atZone(ZoneId.systemDefault()).toInstant())));
			 markupConfig.updateByPrimaryKey(config);
			 Integer markup_id=config.getMarkup_id();
	         //第三条
	         setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setPageIndex(2);
	         bean.setPageSize(1);
	         
			 call.setData(bean);
		 	 call.callService();			
			
		 	 
		 	 Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			 JSONArray jArray=call.getList();
			 Logger.verifyEquals(3, call.getTotal(), "验证total与返回的一致");

				 markupConfig=new markup_config_conditionWapper();
				 example=new markup_config_conditionExample();
				example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId).andMarkup_idEqualTo(markup_id);
				example.setOrderByClause(" create_time desc");
				List<markup_config_condition>  configs=markupConfig.selectByExample(example);
			//	for(int i=0;i<configs.size();i++){
					config=configs.get(0);
					JSONObject job=jArray.getJSONObject(0);
					 Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
					
					 Logger.verifyEquals(Integer.parseInt(job.get("markupType").toString()), config.getMarkup_type(), "验证数据库中的Markup_type与返回的一致");
					 Logger.verifyEquals(new BigDecimal(job.get("percentage").toString()).stripTrailingZeros().toPlainString(), config.getPercentage().stripTrailingZeros().toPlainString(), "验证数据库中的percentage与返回的一致");
					 Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
					 Logger.verifyEquals(Integer.parseInt(job.get("onSaleType").toString()), config.getOn_sale_type(), "验证数据库中的onSaleType与返回的一致");
					 Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
				//}
				
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_004() {
		Logger.start(true,"按条件自动加价配置列表-最后一页显示-返回成功");
		try {
			Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
			  //第一条
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         //修改时间
	         updateTimeConfig(agentId,supplierId);
	         //第二条
	         setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         setBean.setCategoryList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         setBean.setDepotList(Lists.newArrayList(Integer.parseInt(TestDataManager.getRandomNum(5)),Integer.parseInt(TestDataManager.getRandomNum(5))));
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	         
	         bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setPageIndex(2);
	         bean.setPageSize(1);
	         
			 call.setData(bean);
		 	 call.callService();	
		 	 
		 	 Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			 JSONArray jArray=call.getList();
			 Logger.verifyEquals(2, call.getTotal(), "验证total与返回的一致");

				markup_config_conditionWapper markupConfig=new markup_config_conditionWapper();
				markup_config_conditionExample example=new markup_config_conditionExample();
				example.createCriteria().andAgent_idEqualTo(agentId).andSupplier_idEqualTo(supplierId);
				example.setOrderByClause(" create_time desc");
				List<markup_config_condition>  configs=markupConfig.selectByExample(example);
			//	for(int i=0;i<configs.size();i++){
					markup_config_condition config=configs.get(1);
					JSONObject job=jArray.getJSONObject(0);
					 Logger.verifyEquals(Integer.parseInt(job.get("markupId").toString()), config.getMarkup_id(), "验证数据库中的markupId与返回的一致");
					
					 Logger.verifyEquals(Integer.parseInt(job.get("markupType").toString()), config.getMarkup_type(), "验证数据库中的Markup_type与返回的一致");
					 Logger.verifyEquals(new BigDecimal(job.get("percentage").toString()).stripTrailingZeros().toPlainString(), config.getPercentage().stripTrailingZeros().toPlainString(), "验证数据库中的percentage与返回的一致");
					 Logger.verifyEquals(Integer.parseInt(job.get("decimalType").toString()), config.getDecimal_type(), "验证数据库中的decimalType与返回的一致");
					 Logger.verifyEquals(Integer.parseInt(job.get("onSaleType").toString()), config.getOn_sale_type(), "验证数据库中的onSaleType与返回的一致");
					 Logger.verifyEquals(job.get("refreshProduct"), config.getRefresh_product(), "验证数据库中的refreshProduct与返回的一致");
				//}
				
					 
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_005() {
		Logger.start(true,"按条件自动加价配置列表-代理商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	           
	        // bean.setAgentId(agentId);
	         bean.setSupplierId(supplierId);
	         bean.setPageIndex(1);
	         bean.setPageSize(2);
	         
			 call.setData(bean);
		 	 call.callService();			
			
			 Logger.verifyEquals("4001", call.getString("status"), "验证code");
				
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getConditionMarkupConfigList_006() {
		Logger.start(true,"按条件自动加价配置列表-供货商id为空-返回失败");
		try {
			 Integer agentId=GodmchManageCallService.createAgent();
	         Integer supplierId=GodmchManageCallService.createApplySupplier().getSupplierId();
				//新增
	         setConditionMarkupConfigBean setBean=GodmchManageCallService.initSetConditionMarkupConfig(agentId,supplierId);
	         GodmchManageCallService.setConditionMarkupConfigCall(setBean);
	           
	         bean.setAgentId(agentId);
	        // bean.setSupplierId(supplierId);
	         bean.setPageIndex(1);
	         bean.setPageSize(2);
	         
			 call.setData(bean);
		 	 call.callService();			
			
			 Logger.verifyEquals("4001", call.getString("status"), "验证code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}